package com.nettest.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.text.StrTokenizer;

public class StringUtils extends org.apache.commons.lang.StringUtils {

	public static final String SPACE_DELIMIT = " ";
	public static final String DEFAULT_DELIMIT = ",";
	public static final String DEFAULT_QUOTE = "";
	public static int BYTES_PREHZ_IN_DB=3; //3 bytes pre hz in utf8 db charset
	public static String encodeForHTML(String src) {
		if (src == null)
			return "";

		StringBuilder result = new StringBuilder();

		if (src != null) {
			src = src.trim();
			for (int pos = 0; pos < src.length(); pos++) {
				char t = src.charAt(pos - 1 == -1 ? 0 : pos - 1);
				switch (src.charAt(pos)) {
				case '\"':
					result.append("&quot;");
					break;

				case '<':
					result.append("&lt;");
					break;

				case '>':
					result.append("&gt;");
					break;

				case '&':
					result.append("&amp;");
					break;

				case ' ':
					if (t == ' ') {
						result.append("&nbsp;");
						break;
					}

				default:
					result.append(src.charAt(pos));
					break;

				}
			}
		}

		return result.toString().replace("\n", "<br/>");
	}

	public static String encoding(String src) {
		if (src == null)

			return "";

		StringBuilder result = new StringBuilder();

		if (src != null) {

			src = src.trim();

			for (int pos = 0; pos < src.length(); pos++) {

				switch (src.charAt(pos)) {

				case '\"':
					result.append("&quot;");
					break;

				case '<':
					result.append("&lt;");
					break;

				case '>':
					result.append("&gt;");
					break;

				case '&':
					result.append("&amp;");
					break;
				case ' ':
					result.append("&nbsp;");
					break;
				default:
					result.append(src.charAt(pos));
					break;

				}
			}
		}
		return result.toString();
	}

	// 反过滤特殊字�?

	public static String decoding(String src) {

		if (src == null)

			return "";

		src = src.replace("&quot;", "\"")
				.replace("&lt;", "<")
				.replace("&gt;", ">")
				.replace("&amp;", "&")
				.replace("&nbsp;", " ");
		return src;

	}

	public static String buildTimeBaseNumKey() {
		return ("" + System.currentTimeMillis() + "" + ((int) (Math.random() * 100000)));
	}

	public static String buildRandomKey(int pwd_len) {
		return RandomStringUtils.randomAlphanumeric(8).toUpperCase();
	}

	public static String cutFirstChar(String str) {
		if (str == null || "".equals(str)) {
			return str;
		}
		return str.substring(1);
	}

	public static String cutLastChar(String str) {
		if (str == null || "".equals(str)) {
			return str;
		}
		return str.substring(0, str.length() - 1);
	}

	public static String nvl(Object value, String defaultValue) {
		if (CommonUtil.isNull(value)) {
			return defaultValue;
		}
		return value.toString();
	}

	public static String toStr(List<String> source, String separator) {
		return join(source, separator);
	}

	@SuppressWarnings("unchecked")
	public static List<String> toList(String source, String delim) {
		StrTokenizer tokenizer = new StrTokenizer(source, delim);
		return tokenizer.getTokenList();
	}

	/**
	 * 
	 * 按指定长度和编码拆分中英混合字符串
	 * @param text
	 *            被拆分字符串
	 * @param length
	 *            指定长度，即子字符串的最大长度
	 * @param encoding
	 *            字符编码
	 * @return 拆分后的子字符串列表
	 * @throws UnsupportedEncodingException
	 */

	public static String[] split(String text, int length, String encoding)
	throws UnsupportedEncodingException {
		List<String> texts = new ArrayList<String>();
		int pos = 0;
		int startInd = 0;
		for (int i = 0; text != null && i < text.length();) {
			byte[] b = String.valueOf(text.charAt(i)).getBytes(encoding);
			if (b.length > length) {
				i++;
				startInd = i;
				continue;
			}
			pos += b.length;
			if (pos >= length) {
				int endInd;
				if (pos == length) {
					endInd = ++i;
				} else {
					endInd = i;
				}
				texts.add(text.substring(startInd, endInd));
				pos = 0;
				startInd = i;
			} else {
				i++;
			}
		}
		if (startInd < text.length()) {
			texts.add(text.substring(startInd));
		}

		return (String[])texts.toArray(new String[]{});

	}

	/**
	 * 指定长度按UTF-8编码拆分中英混合字符串，即一个非ASCII码长度为3
	 * @param text
	 *            被拆分字符串
	 * @param length
	 *            指定长度，即子字符串的最大长度
	 * @return
	 */

	public static String[] split(String text, int len) {
		try {
			return split(text, len, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static int getStringLength(String str) {
		int chineseNumber = 0;
		int englishNumber = 0;
	    Integer strLen = 0;
	    
		String regEx = "[^\\x00-\\xff]";
	    Pattern p = Pattern.compile(regEx);
	    Matcher m = p.matcher(str);
		while (m.find()) {
		   chineseNumber ++;
 		}   
 		    
 		regEx = "[\\x00-\\xff]";
 		p = Pattern.compile(regEx);
 		m = p.matcher(str);
 		while (m.find()) {
		   englishNumber ++;
 		}   
         
 		strLen = chineseNumber * BYTES_PREHZ_IN_DB + englishNumber;
 		return strLen;
	}
	public static void main(String[] args) {
		System.out.println(buildRandomKey(8));
	}
}
